package com.woniu.houtai.utils;

import com.woniu.houtai.config.Converter;
import com.woniu.houtai.entity.Business;
import com.woniu.houtai.entity.Shop;
import com.woniu.houtai.entity.Type;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.*;
import java.nio.file.Files;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.UUID;

public class FileUtils {
    //拷贝文件
    public static String copy(MultipartFile imageUrl, String filePath) throws IOException {


//        String ip = "192.168.201.161"; //目标ip
//        String filePath = "//"+ip+"/imgs/";

        // 图片保存
        String uuid = UUID.randomUUID().toString().replace("-", "").toUpperCase();
        String suffix = uuid.substring(0, 4);
        String prefix = Converter.toDate(new Date());
        String proPicCode = prefix + suffix;
        String fileName = Objects.requireNonNull(imageUrl.getOriginalFilename()).toUpperCase();
        String[] strings=Objects.requireNonNull(imageUrl.getOriginalFilename()).split("\\.");
        String fileType = strings[strings.length-1];
        System.out.println(fileName);
        String newFilename = proPicCode + "-" + fileName.substring(0,1)+"."+fileType;
        File file = new File(filePath + newFilename);
        BufferedInputStream inputStream = new BufferedInputStream(imageUrl.getInputStream());
        BufferedOutputStream outputStream = new BufferedOutputStream(Files.newOutputStream(file.toPath()));
        IOUtils.copy(inputStream, outputStream);
        outputStream.close();
        inputStream.close();
        return newFilename;

    }

    //
}
